package com.yht.downloadimage.download.file;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.impl.client.CloseableHttpClient;

import com.yht.downloadimage.requestMethod.get.GetRequest;

public class DownLoadFile {
	public   void downloadImage(CloseableHttpClient httpClient,String url,RequestConfig requestConfig,Map<String,String> requestParams,String file) throws Exception{
		CloseableHttpResponse response = GetRequest.class.newInstance().request(httpClient, url, requestConfig, requestParams);
		if (response.getStatusLine().getStatusCode()!= org.apache.http.HttpStatus.SC_OK) {
			throw new Exception("无法文件["+url+"]到["+file+"]");
		}
		InputStream inputStream = response.getEntity().getContent();
		OutputStream outStream = new FileOutputStream(file);
		IOUtils.copy(inputStream, outStream);
		if(outStream!=null){
			outStream.close();
		}
		if(inputStream!=null){
			inputStream.close();
		}
		if(response!=null){
			response.close();
		}
	}
}
